﻿using System;
using System.Web;
using System.Web.UI;
using SimplePMT.Data;
using SimplePMT.Control;
using System.Text;
using System.Collections.Generic;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace SimplePMT
{
    public partial class TaskContainerView : Components.BasePage
    {
        
        public string taskContainerView = string.Empty;
        public string taskContainerId = string.Empty;
        public string memberId = string.Empty;
        public string parentContainerId = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidateSession();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(TaskContainerView), this.Page);
            
            Control.TaskContainerControl view = this.LoadControl("./Control/TaskContainerControl.ascx") as Control.TaskContainerControl;
            if (Session["moveTaskBuffer"] != null)
            {
                view.moveTasks = Session["moveTaskBuffer"] as List<Task>;
            }
            view.edit = true;
            parentContainerId = Request.QueryString["pid"];
            taskContainerId = Request.QueryString["id"];
            view.taskContainerID = Convert.ToInt32(taskContainerId);
            view.parentTaskContainerID = Convert.ToInt32(parentContainerId);
            taskContainerId = view.taskContainerID.ToString();
            taskContainerView = view.ToString();
            memberId = Session["MemberID"].ToString();
        }
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public bool MoveTasksToBuffer(int[] taskIdList)
        {
            List<Task> taskList = new List<Task>();
            foreach (int taskId in taskIdList)
            {
                taskList.Add(Task.Find(taskId));
            }
            if (!taskList.Count.Equals(0))
            {
                Session["moveTaskBuffer"] = taskList;
            }
            return true;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public bool MoveTasksFromBuffer(int containerId, int[] taskIdList)
        {
            foreach (int taskId in taskIdList)
            {
                TaskContainerItem[] tci = TaskContainerItem.FindAllByProperty("TaskID", taskId);
                foreach (TaskContainerItem itm in tci)
                {
                    itm.TaskContainer = TaskContainer.Find(containerId);
                    itm.Save();
                }
            }
            Session.Remove("moveTaskBuffer");
            return true;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public bool ClearTasksFromBuffer()
        {
            Session.Remove("moveTaskBuffer");
            return true;
        }

        [AjaxPro.AjaxMethod()]
        public bool DeleteTaskContainer(int taskContainerId)
        {
            TaskContainer container = TaskContainer.Find(taskContainerId);
            container.Delete();
            return true;
        }

        [AjaxPro.AjaxMethod()]
        public object[] SaveTaskContainer(int taskContainerId, int parentContainerId, string taskContainerLabel, string taskContainerDescription, int createMemberId, bool includeInProjectPlan)
        {
            TaskContainer taskContainer = TaskContainer.Find(taskContainerId) ?? new TaskContainer();
            taskContainer.Label = taskContainerLabel.Replace("&quot;", "\"");
            taskContainer.ParentContainerID = parentContainerId;
            taskContainer.Description = taskContainerDescription;
            taskContainer.CreatingMember = Member.Find(createMemberId);
            taskContainer.IncludeInProjectPlan = includeInProjectPlan;
            taskContainer.Save();
            return new object[] {true, parentContainerId, "top.document.img"+ parentContainerId};
        }

        
        
    }
}
